// .txt

beginzonescript;

variables;

short crime_tolerance = 1;
short last_abil;
short i;
short pulse = 0;
short cutscene = 0;
short door_mess = 0;
short pulse_alt = 0;

body;

beginstate INIT_STATE;
	if (gf(60,9) != 3)
		sf(60,21,1);
	if (gf(60,9) > 0)
		sf(60,22,1);
	
	set_obj_act_at_dist(66,1);
	set_obj_act_at_dist(67,1);
	
	if (gf(60,18) > 0) {
		sf(60,25,1);
		sf(60,26,1);
		}
	if (gf(60,17) == 1)
		sf(60,17,0);
	if (gf(60,16) == 0)
		sf(60,17,0);
		
	set_name(37,"Commander Elysia");
	add_range_to_group(37,44,6);

	set_name(57,"Servile Technician");
	set_name(58,"Servile Technician");
	set_name(54,"Unbound Prototype");
	set_name(55,"Unbound Prototype");
	set_name(63,"Cultist Guard");
	set_name(64,"Cultist Guard");
	set_name(65,"Cultist Guard");

	set_name(68,"Greta");
	set_level(68,30);
	set_boss_level(68,2);
	if (gf(101,8) > 0)
		erase_char(68);

	set_name(72,"Mehken");
	set_level(72,20);
	set_boss_level(72,2);
	if (gf(101,0) > 0) {
		erase_char(72);
		}
		
	set_name(56,"Saakash");
	set_boss_level(56,2);

	set_name(73,"Elik");
	set_name(74,"Phyllida");
	set_boss_level(74,1);
	set_name(75,"Smith Drossch");
	set_name(76,"Khressia");
	set_name(77,"Muttering Drakon");
	if (gf(60,16) > 0) {
		erase_char(76);
		erase_char(77);
		}

	if (gf(100,23) == 0) {
		erase_char(19);
		erase_char(20);
		erase_char(21);
		erase_char(22);
		erase_char(23);
		erase_char(24);
		erase_char(25);
		erase_char(26);
		erase_char(27);
		erase_char(28);

		erase_char(1006);
		}
	if (gf(100,23) > 0) {
		kill_object(62,1);
		kill_object(63,1);
		kill_object(139,1);
		
		set_terrain(33,54,0);
		set_terrain(33,52,204);
		set_terrain(33,55,205);
		
		set_terrain(38,53,0);
		set_terrain(38,52,204);
		set_terrain(38,55,205);
		
		set_terrain(53,53,0);
		set_terrain(53,52,204);
		set_terrain(53,55,205);
		
		erase_char(54);
		erase_char(55);
		erase_char(59);
		erase_char(60);
		erase_char(66);
		erase_char(68);
		erase_char(72);
		erase_char(74);
		}

	// council dead count
	sf(59,20,0);
	if (gf(101,2) > 0)
		inc_flag(59,20,1);
	if (gf(101,3) > 0)
		inc_flag(59,20,1);
	if (gf(101,4) > 0)
		inc_flag(59,20,1);
	if (gf(101,5) > 0)
		inc_flag(59,20,1);
	
//	add_range_to_group(first,last,whichg);

	if ((gf(100,5) == 3) && (gf(100,23) == 0)) {
		print_str_color("Somehow, news of your betrayal has gotten here before you.",3);
		print_str_color("You are no longer welcome.",3);
		sf(60,3,1);
		}
		
	if ((get_sdf(60,3) > 0) || (gf(100,23) > 0)) {
		make_zone_hostile();
		}
		else {
			print_str_color("This is a friendly city (for now). You are able to rest.",2);
			revive_party();
			}	

	last_abil = get_current_tick();
	
	set_crime_tolerance(crime_tolerance);
	break;

beginstate EXIT_STATE;

break;

beginstate START_STATE;
	if ((gf(60,3) > 0) && (gf(100,5) == 2)) {
		sf(100,5,3);
		print_str_color("Now that Gazaki-Uss is angry with you, you will no longer be",3);
		print_str_color("  able to serve Ghaldring.",3);
		}


	//if ((gf(60,9) == 1) || ((char_ok(8) == FALSE) && (gf(60,17) != 1)))
	//	sf(60,22,1);
		
	// kill if hostiel too early
	if ((gf(101,10) > 0) || ((gf(60,3) > 0) && (gf(60,1) == 0))) 
		begin_talk_mode(10);
		
	if ((gf(60,17) > 0) && (door_mess == 0)) {
		door_mess = 1;
		give_object_message(66,DOOR_OPEN_MESSAGE);
		give_object_message(67,DOOR_OPEN_MESSAGE);
		}
	
	// make sure ghald door open
	if (gf(60,9) > 4)
		sf(60,21,1);
		
	// clear zone?
	if ((zone_clear(ME) == FALSE) && (get_flag(60,29) > 0)) {
		clear_zone(ME);	
		}
	if ((zone_clear(ME) == FALSE) && (get_flag(60,9) > 13)) {
		clear_zone(ME);	
		}
	
	if ((gf(61,28) > 1) && (get_terrain(27,61) != 0))
		set_terrain(27,61,0);
		
	if ((get_crime_level() >= crime_tolerance) && (get_sdf(60,3) == 0) && (gf(100,23) == 0)) {
		make_zone_hostile();
		set_flag(60,3,1);
		}

	if ((gf(100,23) == 0) && (party_near_nav(0,4)) && (gf(60,18) == 0)) {
		sf(60,18,1);
		sf(60,25,0);
		begin_talk_mode(7);
		}
		
	 if ((gf(60,3) == 0) && (gf(100,23) == 0)) {
		if (get_ran(1,0,100) < 10)
			give_char_text_bubble(68,"Come join me.");
		if (get_ran(1,0,100) < 10)
			give_char_text_bubble(68,"Ghaldring will learn.");
		if (get_ran(1,0,100) < 10)
			give_char_text_bubble(68,"A fresh insult.");
		if (get_ran(1,0,100) < 10)
			give_char_text_bubble(68,"I must return.");
		if (get_ran(1,0,100) < 10)
			give_char_text_bubble(68,"Stay patient ...");
		if (get_ran(1,0,100) < 10)
			give_char_text_bubble(72,"Good to see you again.");
		if (get_ran(1,0,100) < 10)
			give_char_text_bubble(72,"You survived ...");
		if (get_ran(1,0,100) < 10)
			give_char_text_bubble(72,"Must depart soon.");
		if (get_ran(1,0,100) < 10)
			give_char_text_bubble(72,"The Dera Reaches ...");
		if (get_ran(1,0,100) < 10)
			give_char_text_bubble(56,"What was that?");
		if (get_ran(1,0,100) < 10)
			give_char_text_bubble(56,"Get a fresh sample.");
		if (get_ran(1,0,100) < 10)
			give_char_text_bubble(56,"Spies everywhere ...");
		if ((get_ran(1,0,100) < 10) && (gf(60,9) > 5))
			give_char_text_bubble(56,"Please. Waste my time.");
		if (get_ran(1,0,100) < 10)
			give_char_text_bubble(73,"Come and trade.");
		if (get_ran(1,0,100) < 10)
			give_char_text_bubble(73,"I smuggle!");
		if (get_ran(1,0,100) < 10)
			give_char_text_bubble(73,"Good goods!");
		if (get_ran(1,0,100) < 10)
			give_char_text_bubble(74,"Curse them.");
		if (get_ran(1,0,100) < 10)
			give_char_text_bubble(74,"I am useful!");
		if (get_ran(1,0,100) < 10)
			give_char_text_bubble(74,"I will be heard.");
		if (get_ran(1,0,100) < 10)
			give_char_text_bubble(75,"Hssss ...");
		if (get_ran(1,0,100) < 10)
			give_char_text_bubble(75,"Clank! Clank!");
		if (get_ran(1,0,100) < 10)
			give_char_text_bubble(75,"And bend ... yesss ...");
		if (get_ran(1,0,100) < 10)
			give_char_text_bubble(75,"It isss done.");
		if (get_ran(1,0,100) < 10)
			give_char_text_bubble(76 + get_ran(1,0,1),"No. Not yet.");
		if (get_ran(1,0,100) < 10)
			give_char_text_bubble(76 + get_ran(1,0,1),"Yesss ... Soon.");
		if (get_ran(1,0,100) < 10)
			give_char_text_bubble(76 + get_ran(1,0,1),"Mutter. Mutter.");
		if (get_ran(1,0,100) < 10)
			give_char_text_bubble(76 + get_ran(1,0,1),"Someone listensss.");

		}
	
	if (((is_combat()) && (tick_difference(last_abil,get_current_tick()) > 0)) || ((is_group()) && (tick_difference(last_abil,get_current_tick()) > 2))) {
		last_abil = get_current_tick();
		
		if (gf(60,28) > 0)
			inc_flag(60,28,-1);
			
		// PATROL
		inc_flag(60,27,1);
		if (gf(60,27) == 4)
			sf(60,27,0);
			
		// CENTER TRAP
		if (pulse_alt == 0) 
			pulse_alt = 1;
			else {
				pulse_alt = 0;
				pulse = 0;
				if (gf(60,18) > 0) {
					if (gf(60,18) <= 9) {
						inc_flag(60,18,1);
						if (gf(60,18) == 2)
							begin_talk_mode(8);
							else if (gf(60,18) > 2)
								pulse = 12 * (gf(60,18) - 2);
						}
						else if (gf(60,18) == 10) {
							sf(60,18,11);
							sf(60,26,1);
							begin_talk_mode(9);
							}
							else if (gf(60,18) == 11)
								sf(60,18,12);
								else if (gf(60,18) == 12) {
									sf(60,18,11);
									pulse = 15;
									}
					}
				if ((pulse > 0) && (party_dist_to_nav(0) <= 10)) {
					print_str_color("The crystal sends out a pulse of heat, scorching everything near it.",3);
					run_sparkles_on_object(115,1,1,0);
					run_sparkles_on_object(116,1,1,0);
					run_sparkles_on_object(117,1,1,0);
					run_sparkles_on_object(118,1,1,0);
					play_sound(117);
					if (gf(60,18) < 11)
						loc_do_damage(36,31,5,6 + 2 * pulse,2); 
					loc_do_damage(36,31,10,4 + pulse / 2,2);
					}
				}
				
		// ASSAULT
		if ((gf(100,23) > 0) && (gf(60,29) == 0)) {
			if ((char_ok(29) == FALSE) && (get_ran(1,0,1) == 0))
				spawn_creature(21);
			if ((char_ok(30) == FALSE) && (get_ran(1,0,1) == 0))
				spawn_creature(22);
			if ((char_ok(31) == FALSE) && (get_ran(1,0,1) == 0))
				spawn_creature(23);
			if ((char_ok(32) == FALSE) && (get_ran(1,0,1) == 0))
				spawn_creature(24);

			}
		if ((gf(100,23) > 0) && (gf(59,22) > 0)) {
			if ((char_ok(33) == FALSE) && (get_ran(1,0,1) == 0))
				spawn_creature(25);
			if ((char_ok(34) == FALSE) && (get_ran(1,0,1) == 0))
				spawn_creature(26);
			if ((char_ok(35) == FALSE) && (get_ran(1,0,1) == 0))
				spawn_creature(27);
			if ((char_ok(36) == FALSE) && (get_ran(1,0,1) == 0))
				spawn_creature(28);
			}
			
		}
	if (gf(60,17) == 1) {
		if (cutscene == 0) {
			char_clear_action(1000);
			toggle_cutscene(1);	
			cutscene = 1;
	
			// close ghald door
			sf(60,21,0);
			
			sf(60,22,0);
	
			// erase other khres
			erase_char(75);
			erase_char(76);
			erase_char(77);
			}
			else if (cutscene == 1) {
				move_to_loc(0,5,6);
				move_to_loc(1,3,6);
				move_to_loc(2,4,5);
				move_to_loc(3,6,5);
				move_to_loc(4,5,4);
				move_to_loc(5,3,4);
				move_to_loc(6,4,3);
				move_to_loc(7,6,3);
		
				if ((char_ok(0)) && (dist_to_nav_point(0,1) > 20))
					relocate_char(0,5,6,0);
				if ((char_ok(1)) && (dist_to_nav_point(1,1) > 20))
					relocate_char(1,2,6,0);
				if ((char_ok(2)) && (dist_to_nav_point(2,1) > 20))
					relocate_char(2,4,5,0);
				if ((char_ok(3)) && (dist_to_nav_point(3,1) > 20))
					relocate_char(3,6,5,0);
				if ((char_ok(4)) && (dist_to_nav_point(4,1) > 20))
					relocate_char(4,5,4,0);
				if ((char_ok(5)) && (dist_to_nav_point(5,1) > 20))
					relocate_char(5,3,4,0);
				if ((char_ok(6)) && (dist_to_nav_point(6,1) > 20))
					relocate_char(6,4,3,0);
				if ((char_ok(7)) && (dist_to_nav_point(7,1) > 20))
					relocate_char(7,6,3,0);
		
				shift_view_to_char(pc_num());
	
				if (cutscene_can_proceed()) {
					revive_party();
					give_char_text_bubble(1000,"Zzzzz.");
					set_facing(1000,5);
					shift_view_to_char(pc_num());
					cutscene = 2;
					sf(60,22,0);
					}
	
				}
			else if (cutscene == 2) {
				cutscene = 5;
				}
			else if (cutscene == 5) {
				cutscene = 8;
				give_char_text_bubble(1000,"");
				begin_talk_mode(21);
				}
			else if (cutscene == 8) {
				cutscene = 9;
				activate_hidden_group(10);
				add_range_to_group(11,18,7);
				}
			else if (cutscene == 9) {
				move_to_loc(0,16,14);
				move_to_loc(1,15,13);
				move_to_loc(2,17,13);
				move_to_loc(3,16,12);
				move_to_loc(4,14,12);
				move_to_loc(5,18,12);
				move_to_loc(6,15,11);
				move_to_loc(7,17,11);
	
				move_to_loc(8,16,17);
				move_to_loc(9,15,18);
				move_to_loc(10,17,18);
	
				move_to_loc(11,11,13);
				move_to_loc(12,11,14);
				move_to_loc(13,11,15);
				move_to_loc(14,11,16);
	
				move_to_loc(15,21,11);
				move_to_loc(16,22,11);
				move_to_loc(17,21,12);
				move_to_loc(18,22,12);
				
				shift_view_to_char(pc_num());
	
				if (cutscene_can_proceed()) {
					set_facing(1000,3);
					shift_view_to_char(pc_num());
					cutscene = 12;
					}
				}
			else if (cutscene == 12) {
				cutscene = 13;
				begin_talk_mode(30);
				}
			else if (cutscene == 13) {
				// does drakon flee?
				if ((gf(60,16) == 1) || (gf(60,16) == 3)) {
					erase_char(1007);
					cutscene = 15;
					}
					else { // end scene, start fight
						sf(60,17,2);
						sf(60,9,3);
						toggle_cutscene(0);
						shift_view_to_char(pc_num());
						
						}
				}
			else if (cutscene == 15) { // drakon goes
				move_to_loc(8,28,15);
				move_to_loc(9,28,16);
				move_to_loc(10,28,17);
	
				if (cutscene_can_proceed()) {
					spawn_creature(67);
					set_name(75,"Smith Drossch");
		
					erase_char(8);
					erase_char(9);
					erase_char(10);
					
					sf(60,22,1);
					sf(60,23,1);
					
					sf(60,17,2);
					toggle_cutscene(0);
					shift_view_to_char(pc_num());
					sf(60,9,3);
					}
	
				}
		}
		
break;

beginstate 10;
	if ((gf(60,17) == 0) && (gf(100,23) == 0) && (gf(60,28) == 0)) {
		begin_talk_mode(25);

		}
break;

beginstate 11;
	sf(60,29,1);
break;

//	print_str("");
//	set_terrain_string_range(".",3);
//	set_terrain_string_range("The sign says - .",3);
